package com.cman777.video.service.vs;

import com.cman777.springc.common.helper.Base64Utils;
import com.cman777.springc.common.helper.ValidateUtil;
import com.cman777.springc.web.util.RestTemplateUtil;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author chenzhicong
 * @time 2020/12/3 9:52
 */
public class VS147 implements IVideoSource{
    @Override
    public String getDownLoadUrlOfGenuineUrl(String genuineUrl) {
        if(genuineUrl.contains("?")){
            genuineUrl = genuineUrl.substring(0,genuineUrl.indexOf("?"));
        }
        String url = VideoSourceEnum.VS_147_NEW.getOther().concat("?url=").concat(genuineUrl);
        MultiValueMap<String,Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("url",genuineUrl);
        paramMap.add("referer", Base64Utils.encode(url.getBytes(Charset.defaultCharset())));
        paramMap.add("ref",0);
        paramMap.add("time",System.currentTimeMillis()/1000L);
        paramMap.add("type","");
        paramMap.add("other",Base64Utils.encode(genuineUrl.getBytes(Charset.defaultCharset())));
        paramMap.add("ios","");
        Map<String,String> headersMap = new LinkedHashMap<>();
        headersMap.put("Accept","    application/json, text/javascript, */*; q=0.01");
        headersMap.put("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        headersMap.put("Origin","https://jx.wzslw.cn");
        headersMap.put("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");
        headersMap.put("x-requested-with","XMLHttpRequest");
        headersMap.put("sec-fetch-mode","cors");
        headersMap.put("sec-fetch-site","same-origin");
        JsonNode result = RestTemplateUtil.doHttp4JsonBody(VideoSourceEnum.VS_147_NEW.getUrl(), HttpMethod.POST,headersMap,paramMap);
        ValidateUtil.isTrue(result.get("success").asInt() == 1,"解析失败","解析失败");
        return result.get("url").asText();

    }

}
